package com.bw.zg2.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

/**
 *
 * @ClassName: BaseActivity2
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/12 12:02 PM
 * @Version: 1.0
 */
abstract class BaseActivity2<VM : ViewModel, D : ViewDataBinding>(
    private val layoutId: Int,
    private val vm: VM
) : AppCompatActivity() {

    protected lateinit var viewModel: VM
    protected lateinit var dataBinding: D
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding = DataBindingUtil.setContentView(this, layoutId)
        dataBinding.lifecycleOwner = this

        //获取viewModel
        viewModel = ViewModelProvider(this)[vm::class.java]

        initView(savedInstanceState)
        initData()

    }

    abstract fun initData()

    abstract fun initView(savedInstanceState: Bundle?)


    override fun onDestroy() {
        super.onDestroy()
        //防止内存泄漏
        if (dataBinding != null) {
            dataBinding.unbind()
        }
    }
}